package com.stiff.manage.controller;

import com.github.pagehelper.PageInfo;
import com.stiff.common.bean.EasyUIResult;
import com.stiff.manage.pojo.ItemParam;
import com.stiff.manage.service.ItemParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("item/param")
public class ItemParamController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ItemParamController.class);
    @Autowired
    private ItemParamService itemParamService;

    /**
     * 根据类目ID查询模版
     * @param itemCatId
     * @return
     */
    @RequestMapping(value = "{itemCatId}",method = RequestMethod.GET)
    public ResponseEntity<ItemParam> queryByItemId(@PathVariable("itemCatId")Long itemCatId){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【根据类目ID查询模版】itemCatId={}",itemCatId);
            }
            ItemParam record = new ItemParam();
            record.setItemCatId(itemCatId);
            ItemParam itemParam = this.itemParamService.queryOne(record);
            if(null==itemParam){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemParam);
        } catch (Exception e) {
            LOGGER.error("【根据类目ID查询模版失败！】itemCatId="+itemCatId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 新增规格模版
     * @param itemParam
     * @param itemCatId
     * @return
     */
    @RequestMapping(value = "{itemCatId}",method = RequestMethod.POST)
    public ResponseEntity<Void> saveItemParam(ItemParam itemParam,@PathVariable("itemCatId")Long itemCatId){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【新增规格模版】itemCatId={},paramData={}",itemCatId,itemParam.getParamData());
            }
            itemParam.setItemCatId(itemCatId);
            this.itemParamService.save(itemParam);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            LOGGER.error("【新增规格模版失败！】itemCatId="+itemCatId+",param="+itemParam.getParamData(),e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询商品规格模版列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<EasyUIResult> queryItemParamList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                      @RequestParam(value = "rows",defaultValue = "30")Integer rows){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【查询商品规格模版列表】page={}，rows={}",page,rows);
            }
            PageInfo<ItemParam> itemPageInfo = this.itemParamService.queryPageList(page, rows);
            EasyUIResult easyUIResult = new EasyUIResult(itemPageInfo.getTotal(),itemPageInfo.getList());
            return ResponseEntity.ok(easyUIResult);
        } catch (Exception e) {
            LOGGER.error("【查询商品规格模版列表！】page="+page+",rows="+rows,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
